﻿using System;
using System . Collections . Generic;
using System . Linq;
using System . Text;
using System . Threading . Tasks;
using GameClassLibrary;
using System . Reflection;
using System . IO;
using System . Xml . Linq;
using System . Collections . ObjectModel;
using GameClassLibrary . Players;

namespace GameClassLibrary . Players . Models
{
    public class PlayerModel : GameObject
    {
        [SaveValue]
        public string Name { get; set; }

        [SaveValue]
        public string Introduction { get; set; }

        [SaveValue]
        public Uri Model { get; set; }

        [SaveValueList]
        public List<Saying> sayingWhenGained { get; set; } = new List<Saying> ( );

        public Saying SayingWhenGained ( )
        {
            List<Saying> temp=new List<Saying>(from p in sayingWhenGained
                                               where p.Player==null
                                               select p);

            return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
        }

        public Saying SayingWhenGained ( PlayerModel harmed )
        {
            List<Saying> temp=new List<Saying>(from p in sayingWhenGained
                                               where p.Player==harmed.Name
                                               select p);

            if ( temp . Count != 0 )
            {
                return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
            }
            else
            {
                return SayingWhenGained ( );
            }
        }

        [SaveValueList]
        public List<Saying> sayingWhenHarmed { get; set; } = new List<Saying> ( );

        public Saying SayingWhenHarmed ( )
        {
            List<Saying> temp=new List<Saying>(from p in sayingWhenHarmed
                                               where p.Player==null
                                               select p);

            return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
        }

        public Saying SayingWhenHarmed ( PlayerModel gained )
        {
            List<Saying> temp=new List<Saying>(from p in sayingWhenHarmed
                                               where p.Player==gained.Name
                                               select p);

            if ( temp . Count != 0 )
            {
                return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
            }
            else
            {
                return SayingWhenGained ( );
            }
        }

        [SaveValueList]
        public List<Saying> sayingWhenMeet { get; set; } = new List<Saying> ( );

        public Saying SayingWhenMeet ( )
        {
            List<Saying> temp=new List<Saying>(from p in sayingWhenMeet
                                               where p.Player==null
                                               select p);

            return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
        }

        public Saying SayingWhenMeet ( PlayerModel player )
        {
            List<Saying> temp=new List<Saying>(from p in sayingWhenMeet
                                               where p.Player==player.Name
                                               select p);

            if ( temp . Count != 0 )
            {
                return temp [ Game . Current . Randomer . Next ( 0 , temp . Count ) ];
            }
            else
            {
                return SayingWhenMeet ( );
            }
        }

        public override void NextDay ( )
        {

        }

        public PlayerModel ( XElement saving ) : base ( saving )
        {

        }

        public PlayerModel ( PlayerModelProxy proxy )
        {

            if ( null == proxy )
            {
                throw new ArgumentNullException ( "proxy" , "" );
            }

            XDocument doc = ResourceHelper.GetXmlDocument(@"PlayerModel."+ proxy.FileName);

            var modelNode =doc . Root;

            var tempSayingWhenGained=from p in modelNode.Element("SayingWhenGained").Descendants()
                                     select new Saying
                                     {
                                         Text =(string)p.Attribute("Text"),
                                         Expression=(string)p.Attribute("Expression"),
                                         Player=(string)p.Attribute("Player")
                                     };



            sayingWhenGained . AddRange ( tempSayingWhenGained );

            var tempSayingWhenHarmed=from p in modelNode.Element("SayingWhenGained").Descendants()
                                     select new Saying
                                     {
                                         Text =(string)p.Attribute("Text"),
                                         Expression=(string)p.Attribute("Expression"),
                                         Player=(string)p.Attribute("Player")
                                     };

            sayingWhenHarmed . AddRange ( tempSayingWhenHarmed );


            var tempSayingWhenMeet=from p in modelNode.Element("SayingWhenGained").Descendants()
                                   select new Saying
                                   {
                                       Text =(string)p.Attribute("Text"),
                                       Expression=(string)p.Attribute("Expression"),
                                       Player=(string)p.Attribute("Player")
                                   };

            sayingWhenMeet . AddRange ( tempSayingWhenMeet );

        }
    }
}
